package com.yf.exam.modules.trainingarchives.organization.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import com.yf.exam.common.entity.Result;
import com.yf.exam.common.enums.AuditCodeEnum;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.exception.ServiceException;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationAuditDTO;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationDTO;
import com.yf.exam.modules.trainingarchives.organization.entity.Organization;
import com.yf.exam.modules.trainingarchives.organization.service.IOrganizationService;
import com.yf.exam.modules.trainingarchives.organization.vo.OrganizationVO;
import com.yf.exam.modules.user.UserUtils;
import com.yf.exam.modules.trainingarchives.trainingclassinfo.dto.resp.TrainingClassInfoRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 机构管理表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Api(tags={"机构管理"})
@RestController
@RequestMapping("/org/api/organization")
@Slf4j
public class OrganizationController {

    @Resource
    private IOrganizationService baseService;
    /**
     * 添加
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加")
    @PostMapping (value = "/save")
    public Result save(@Valid @RequestBody OrganizationDTO reqDTO) {
        //查询当前机构是否存在
        try {
            baseService.beasVerify(reqDTO.getAccount(),reqDTO.getLicenseCode(),"0");
            //复制参数
            baseService.add(reqDTO);
            return Result.ok();
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }
    @ApiOperation(value = "修改")
    @PostMapping (value = "/update")
    public Result update(@RequestBody OrganizationDTO reqDTO) {
        //查询当前机构是否存在
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "机构id不能为空!");
            baseService.beasVerify(reqDTO.getAccount(),reqDTO.getLicenseCode(),reqDTO.getId());
            //复制参数
            baseService.modify(reqDTO);
            return Result.ok("修改成功！");
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping (value = "/delete")
    public Result delete(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("StudentInfoController.delete.elProjectReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkNotNull(baseIdReqDTO.getId(), "机构id不能为空!");
            Boolean result = baseService.removeById(baseIdReqDTO.getId());
            return Result.ok(result);
        } catch (Exception e) {
            log.error("StudentInfoController.delete.error:{}", e.getMessage(), e);
            return Result.faild("删除机构失败!");
        }
    }

    @ApiOperation(value = "审核")
    @PostMapping (value = "/audit")
    public Result audit(@RequestBody OrganizationAuditDTO reqDTO) {
        //查询当前机构是否存在
        try {
            //复制参数
            baseService.audit(reqDTO);
            return Result.ok("审批成功！");
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }

    @ApiOperation(value = "查询详情")
    @PostMapping (value = "/get")
    public Result<OrganizationVO> get(@RequestBody BaseIdReqDTO reqDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "机构id不能为空!");
        Organization organization = baseService.getById(reqDTO.getId());
        if (organization == null) {
            return Result.faild("机构不存在!");
        }
        OrganizationVO vo = new OrganizationVO();
        BeanUtils.copyProperties(organization,vo);
        return Result.ok(vo);
    }

    @ApiOperation(value = "查询列表")
    @PostMapping (value = "/paging")
    public Result<IPage<OrganizationVO>> list(@RequestBody PagingReqDTO<OrganizationVO> reqDTO) {
        try {
            //分页查询并转换
            reqDTO.getParams().setAuditState(AuditCodeEnum.PASS_AUDIT.code);
            IPage<OrganizationVO> page = baseService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            return Result.faild("查询失败");
        }
    }

    @ApiOperation(value = "审核查询列表")
    @PostMapping (value = "/auditPaging")
    public Result<IPage<OrganizationVO>> auditList(@RequestBody PagingReqDTO<OrganizationVO> reqDTO) {
        try {
            //分页查询并转换
            reqDTO.getParams().setAuditState(AuditCodeEnum.WAIT_AUDIT.code);
            IPage<OrganizationVO> page = baseService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            return Result.faild("查询失败");
        }
    }

    /**
     * 基础信息详情
     */
    @PostMapping("/orgList")
    @ApiOperation(value = "获取机构数据")
    public Result<List<OrganizationVO>> getOrgList() {
        try {
            return Result.ok(baseService.getOrgList());
        } catch (Exception e) {
            log.error("OrganizationController.getOrgList.error:{}", e.getMessage(), e);
            return Result.faild("查询机构数据失败!");
        }
    }


}
